<?php
namespace App\Http\Controllers;


use App\Models\User;
use Illuminate\Support\Facades\Auth;
use App\ErrorCode;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Query\Builder;

class TutorController extends Controller
{
    public function __construct(Request $request)
    {
        $this->setParam($request);
    }

    /** 获取导师列表
     * @return array
     */
    public function getTutorList()
    {
        $start = $this->getParam('start');
        $num = $this->getParam('num');
        if (empty($start) && empty($num)) {
            return $this->output(false, ErrorCode::PARAM_ERROR);
        }
        $lists = DB::table('tutor');
        $lists->select('id', 'name','headPic as PicUrl', 'position','products', 'summary', 'headPic', 'createTime');
        $lists->orderby('createTime', 'desc');
        if ((int)$num !== -1) {
            $lists = $lists->skip($start)->take($num)->get();
        } else {
            $lists = $lists->get();
        }
        $lists = json_decode(json_encode($lists), 1);
//        $aa = explode(',',$lists[0]['products']);
//        var_dump($aa);exit();
//        foreach ($lists as $value => $list) {
//            $product = DB::table('tutorProduct')
//                ->where('tutorId', $list['id'])
//                ->select('product')
//                ->get();
//            $product = json_decode(json_encode($product), 1);
//            $temp = [];
//            foreach ($product as $item) {
//                array_push($temp, $item['product']);
//            }
//            $lists[$value]['productList'] = $temp;
//        }
        $res['count'] = count($lists);
        $res['items'] = $lists;

        return $this->output($res);
    }

    /** 创建导师
     * @return array
     */
    public function createTutor()
    {
        $name = $this->getParam('name');
        $position = $this->getParam('position');
        $summary = $this->getParam('summary');
        $headPic = $this->getParam('headPic');
        $products = $this->getParam('products');

        if (empty($name) || empty($position) || empty($products) || empty($summary)) {
            return $this->output(false, ErrorCode::PARAM_ERROR);
        }

        $tutorId = DB::table('tutor')
            ->insertGetId([
                'name' => $name,
                'position' => $position,
                'products' => $products,
                'summary' => $summary,
                'headPic' => $headPic,
                'createTime' => time()
            ]);
        if (!$tutorId) {
            return $this->output(false, ErrorCode::OPERATION_ERROR);
        }
//        foreach ($products as $product){
//            $bool = DB::table('tutorProduct')
//                ->insert([
//                    'tutorId' => $tutorId,
//                    'product' => $product,
//                    'createTime' => time()
//                ]);
//            if (!$bool) {
//                return $this->output(false, ErrorCode::OPERATION_ERROR);
//            }
//        }
        return $this->output('success');
    }

    /** 修改导师
     * @return array
     */
    public function updateTutor()
    {
        $tutorId = $this->getParam('tutorId');
        $name = $this->getParam('name');
        $position = $this->getParam('position');
        $summary = $this->getParam('summary');
        $headPic = $this->getParam('headPic');
        $products = $this->getParam('products');

        if (empty($name) || empty($position) || empty($summary) || empty($products)) {
            return $this->output(false, ErrorCode::PARAM_ERROR);
        }

        $num = DB::table('tutor')
            ->where('id', $tutorId)
            ->update([
                'name' => $name,
                'position' => $position,
                'products' => $products,
                'summary' => $summary,
                'headPic' => $headPic,
                'updateTime' => time()
            ]);
        if (!$num) {
            return $this->output(false, ErrorCode::OPERATION_ERROR);
        }
//        $num = DB::table('tutorProduct')
//            ->where('tutorId', $tutorId)
//            ->delete();
//        if (!$num) {
//            return $this->output(false, ErrorCode::OPERATION_ERROR);
//        }
//        foreach ($products as $product){
//            $bool = DB::table('tutorProduct')
//                ->insert([
//                    'tutorId' => $tutorId,
//                    'product' => $product,
//                    'createTime' => time()
//                ]);
//            if (!$bool) {
//                return $this->output(false, ErrorCode::OPERATION_ERROR);
//            }
//        }
        return $this->output('success');
    }

    /** 删除导师
     * @return array
     */
    public function delTutor(){
        $tutorId = $this->getParam('tutorId');
        if(empty($tutorId)){
           return $this->output(false,ErrorCode::PARAM_ERROR);
        }
        $tutorNum = DB::table('tutor')
            ->where('id', $tutorId)
            ->delete();
        if(!$tutorNum){
            return $this->output(false,ErrorCode::OPERATION_ERROR);
        }
        return $this->output('success');
    }

    /** 获取前台导师列表
     * @return array
     */
    public function getWebTutorList(){
        $lists = DB::table('tutor');
        $lists->select('id', 'name', 'position','products', 'summary', 'headPic as PicUrl', 'createTime');
        $lists->orderby('createTime', 'desc');
        $lists = $lists->get();
        $lists = json_decode(json_encode($lists), 1);
        foreach ($lists as $index => $list){
            $temp = explode('，',$list['products']);
            $temp1 = [];
            array_walk($temp, function ($value)use(&$temp1){
                $temp1[]['name'] = $value;
            });

            $temp2 = explode('，',$list['position']);
            $temp3 = [];
            array_walk($temp2, function ($value)use(&$temp3){
                $temp3[]['name'] = $value;
            });


            $lists[$index]['products'] = $temp1;
            $lists[$index]['position'] = $temp3;
        }
        return $this->output($lists);
    }
}